/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.e2e.haiti.infrastructure.surveyor;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;



/**
 * Demonstrates combining a TabHost with a ViewPager to implement a tab UI
 * that switches between tabs and also allows the user to perform horizontal
 * flicks to move between the tabs.
 */
public class Questionnaire2 extends SherlockFragmentActivity {
	
    public static Utils utilsHelper = null;
    private Cursor structureCursor = null;
    public static int structureID;
    
    private static final int STRUCTURE_NOTE_DIALOG_ID = 0;
    
    private static final int SECONDARY_SYSTEM_TAB = 0;
    
    private static final int NOTES_ACTION_MENU = 0;
    private static final int NEXT_ACTION_MENU = 1;
    
    
    TabHost mTabHost;
    ViewPager  mViewPager;
    TabsAdapter mTabsAdapter;
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
        menu.add(0,NOTES_ACTION_MENU,0, "Notes")
        .setIcon(R.drawable.notes)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 

        menu.add(0,NEXT_ACTION_MENU,0, "Next")
            .setIcon(R.drawable.next)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        
       return true; 
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        structureID = getIntent().getIntExtra("structureID", -1); //Get structureID otherwise -1  
        utilsHelper = new Utils(this);      
        structureCursor = utilsHelper.getStructureCursor(structureID);
        
        // Sets the title on the action bar to the be the name of the structure
        String structureTitle = " " + structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NAME)) + " Questionnaire";
        setTitle(structureTitle);   
        
        // Sets the view of the base activity
        setContentView(R.layout.fragment_tabs_pager);
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();

        mViewPager = (ViewPager)findViewById(R.id.pager);

        mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);

        // TABS are created and linked to various classses
        // "q"+Integer.toString(DEMOGRAPHICS_TAB) is a way of specifiy q0,q1,q2 TAGS that may be accessed. These tags are currently not used
        mTabsAdapter.addTab(mTabHost.newTabSpec("q"+Integer.toString(SECONDARY_SYSTEM_TAB)).setIndicator("Secondary"),
        		FragmentSecondaryQuestionnaire.class, null);  

        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case NOTES_ACTION_MENU:
            	showDialog(STRUCTURE_NOTE_DIALOG_ID);           	                       	            
                return true;  
            case NEXT_ACTION_MENU:
            	
                int tabID = mTabHost.getCurrentTab();
                
                // If its not the first time the application enters the Questionnaire tab, update the Formd data to the database
                    Fragment f = (Fragment) mViewPager.getAdapter().instantiateItem(mViewPager, tabID);
                	updateFormDataDB(f.getView() , tabID);
                	//updateFormDataDB(getItem(position).getView());            
            	
	            utilsHelper.startNextActivity(structureID, Exterior.class);
                return true;                  
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("tab", mTabHost.getCurrentTabTag());
    }
    
    /**
     * This is a helper class that implements the management of tabs and all
     * details of connecting a ViewPager with associated TabHost.  It relies on a
     * trick.  Normally a tab host has a simple API for supplying a View or
     * Intent that each tab will show.  This is not sufficient for switching
     * between pages.  So instead we make the content part of the tab host
     * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
     * view to show as the tab content.  It listens to changes in tabs, and takes
     * care of switch to the correct paged in the ViewPager whenever the selected
     * tab changes.
     */
    
    
    public static class TabsAdapter extends FragmentPagerAdapter
            implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
        private final Context mContext;
        private final TabHost mTabHost;
        private final ViewPager mViewPager;
        public int previousTabID = -1; // initialized to -1 when no tabs are selected
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        static final class TabInfo {
            private final String tag;
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(String _tag, Class<?> _class, Bundle _args) {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }

        static class DummyTabFactory implements TabHost.TabContentFactory {
            private final Context mContext;

            public DummyTabFactory(Context context) {
                mContext = context;
            }

            @Override
            public View createTabContent(String tag) {
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }

        public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mTabHost = tabHost;
            mViewPager = pager;
            mTabHost.setOnTabChangedListener(this);
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
            tabSpec.setContent(new DummyTabFactory(mContext));
            String tag = tabSpec.getTag();

            TabInfo info = new TabInfo(tag, clss, args);
            mTabs.add(info);
            mTabHost.addTab(tabSpec);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mTabs.size();
        }

        @Override
        public Fragment getItem(int position) {
            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext, info.clss.getName(), info.args);
        }

        @Override
        // Positon is integer 0-N indiciating which tab
        // tabID is the lowercase name of the Tab
        public void onTabChanged(String tabId) {
            int position = mTabHost.getCurrentTab();
            
            // If its not the first time the application enters the Questionnaire tab, update the Formd data to the database
            if (previousTabID != -1) {
                Fragment f = (Fragment) mViewPager.getAdapter().instantiateItem(mViewPager, previousTabID);
            	updateFormDataDB(f.getView(), previousTabID);
            	//updateFormDataDB(getItem(position).getView());
            }
                        
            previousTabID = position; // initialized to -1 when first enter into Questionnaire class
            mViewPager.setCurrentItem(position);

        }
        
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            // Unfortunately when TabHost changes the current tab, it kindly
            // also takes care of putting focus on it when not in touch mode.
            // The jerk.
            // This hack tries to prevent this from pulling focus out of our
            // ViewPager.
            TabWidget widget = mTabHost.getTabWidget();
            int oldFocusability = widget.getDescendantFocusability();
            widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            mTabHost.setCurrentTab(position);
            widget.setDescendantFocusability(oldFocusability);
        }
        
        @Override
        public void onPageScrollStateChanged(int state) {
        }

    } // END OF TABS ADAPTER
    
    public static void updateFormDataDB(View v, int TabID){
    	
    	// Switches to the correct DB update information given the previous tab
   switch (TabID){
   
   		case SECONDARY_SYSTEM_TAB: 
   			
// Secondary System Questionnaire ==============================================================================
   		// Declares the text input fields
   	        EditText EditText_PermanentInteriorPartitionsNote = (EditText) v.findViewById(R.id.Secondary_editText_PermanentInteriorPartitionsNote); 
   	        EditText EditText_SemiPermanentInteriorPartitionsNote = (EditText) v.findViewById(R.id.Secondary_editText_SemiPermanentInteriorPartitionsNote); 
   	        EditText EditText_FoundationNote = (EditText) v.findViewById(R.id.Secondary_editText_FoundationNote);
   	        EditText EditText_FoundationConstructionMaterialNote =(EditText) v.findViewById(R.id.Secondary_editText_FoundationConstructionMaterialNote);
   	        EditText EditText_RoofSystemTypeNote = (EditText) v.findViewById(R.id.Secondary_editText_RoofSystemTypeNote); 
   	        EditText EditText_ConstructionMaterialFloor1Note = (EditText) v.findViewById(R.id.Secondary_editText_ConstructionMaterialFloor1Note); 
   	        EditText EditText_ConstructionMaterialFloor2Note = (EditText) v.findViewById(R.id.Secondary_editText_ConstructionMaterialFloor2Note);
   	        EditText EditText_ConstructionMaterialFloor3Note = (EditText) v.findViewById(R.id.Secondary_editText_ConstructionMaterialFloor3Note); 
   	        EditText EditText_ConstructionMaterialFloor4Note = (EditText) v.findViewById(R.id.Secondary_editText_ConstructionMaterialFloor4Note);
   	        EditText EditText_ConstructionMaterialFloor5Note = (EditText) v.findViewById(R.id.Secondary_editText_ConstructionMaterialFloor5Note); 
   	        
   	    	// Declares the check box fields
   	        CheckBox CheckBox_PermanentInteriorPartitions_Masonry = (CheckBox) v.findViewById(R.id.Secondary_checkBox_PermanentInteriorPartitions_Masonry);
   	        CheckBox CheckBox_PermanentInteriorPartitions_Stone = (CheckBox) v.findViewById(R.id.Secondary_checkBox_PermanentInteriorPartitions_Stone);
   	        CheckBox CheckBox_PermanentInteriorPartitions_Earth = (CheckBox) v.findViewById(R.id.Secondary_checkBox_PermanentInteriorPartitions_Earth);
   	        CheckBox CheckBox_PermanentInteriorPartitions_Wood = (CheckBox) v.findViewById(R.id.Secondary_checkBox_PermanentInteriorPartitions_Wood);
   	        CheckBox CheckBox_PermanentInteriorPartitions_Metal = (CheckBox) v.findViewById(R.id.Secondary_checkBox_PermanentInteriorPartitions_Metal);
   	        CheckBox CheckBox_PermanentInteriorPartitions_Other = (CheckBox) v.findViewById(R.id.Secondary_checkBox_PermanentInteriorPartitions_Other);
   	        CheckBox CheckBox_SemiPermanentInteriorPartitions_Wood = (CheckBox) v.findViewById(R.id.Secondary_checkBox_SemiPermanentInteriorPartitions_Wood);
   	        CheckBox CheckBox_SemiPermanentInteriorPartitions_Canvas = (CheckBox) v.findViewById(R.id.Secondary_checkBox_SemiPermanentInteriorPartitions_Canvas);
   	        CheckBox CheckBox_SemiPermanentInteriorPartitions_Other = (CheckBox) v.findViewById(R.id.Secondary_checkBox_SemiPermanentInteriorPartitions_Other);
   	        CheckBox CheckBox_Foundation_None = (CheckBox) v.findViewById(R.id.Secondary_checkBox_Foundation_None);
   	        CheckBox CheckBox_Foundation_Mat = (CheckBox) v.findViewById(R.id.Secondary_checkBox_Foundation_Mat);
   	        CheckBox CheckBox_Foundation_Footings = (CheckBox) v.findViewById(R.id.Secondary_checkBox_Foundation_Footings);
   	        CheckBox CheckBox_Foundation_ShallowFoundation = (CheckBox) v.findViewById(R.id.Secondary_checkBox_Foundation_ShallowFoundation);
   	        CheckBox CheckBox_Foundation_DeepFoundation = (CheckBox) v.findViewById(R.id.Secondary_checkBox_Foundation_DeepFoundation);
   	        CheckBox CheckBox_Foundation_ElevatedOnPiles = (CheckBox) v.findViewById(R.id.Secondary_checkBox_Foundation_ElevatedOnPiles);
   	        CheckBox CheckBox_Foundation_Other = (CheckBox) v.findViewById(R.id.Secondary_checkBox_Foundation_Other);
   	        CheckBox CheckBox_FoundationConstructionMaterial_NA= (CheckBox) v.findViewById(R.id.Secondary_checkBox_FoundationConstructionMaterial_NA);
   	        CheckBox CheckBox_FoundationConstructionMaterial_Masonry = (CheckBox) v.findViewById(R.id.Secondary_checkBox_FoundationConstructionMaterial_Masonry);
   	        CheckBox CheckBox_FoundationConstructionMaterial_Stone = (CheckBox) v.findViewById(R.id.Secondary_checkBox_FoundationConstructionMaterial_Stone);
   	        CheckBox CheckBox_FoundationConstructionMaterial_CastConcrete = (CheckBox) v.findViewById(R.id.Secondary_checkBox_FoundationConstructionMaterial_CastConcrete);
   	        CheckBox CheckBox_FoundationConstructionMaterial_Wood = (CheckBox) v.findViewById(R.id.Secondary_checkBox_FoundationConstructionMaterial_Wood);
   	        CheckBox CheckBox_FoundationConstructionMaterial_Metal = (CheckBox) v.findViewById(R.id.Secondary_checkBox_FoundationConstructionMaterial_Metal);
   	        CheckBox CheckBox_FoundationConstructionMaterial_Other = (CheckBox) v.findViewById(R.id.Secondary_checkBox_FoundationConstructionMaterial_Other);
   	        CheckBox CheckBox_RoofSystem_ConcreteSlab = (CheckBox) v.findViewById(R.id.Secondary_checkBox_RoofSystem_ConcreteSlab);
   	        CheckBox CheckBox_RoofSystem_Framed = (CheckBox) v.findViewById(R.id.Secondary_checkBox_RoofSystem_Framed);
   	        CheckBox CheckBox_RoofSystem_Other = (CheckBox) v.findViewById(R.id.Secondary_checkBox_RoofSystem_Other);
   	        CheckBox CheckBox_ConcreteSlabRoofDetails_MasonryPresence = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConcreteSlabRoofDetails_MasonryPresence);
   	        CheckBox CheckBox_FramedRoofDetails_MetalFraming = (CheckBox) v.findViewById(R.id.Secondary_checkBox_FramedRoofDetails_MetalFraming);
   	        CheckBox CheckBox_FramedRoofDetails_WoodFraming = (CheckBox) v.findViewById(R.id.Secondary_checkBox_FramedRoofDetails_WoodFraming);
   	        CheckBox CheckBox_FramedRoofDetails_MetalCovering = (CheckBox) v.findViewById(R.id.Secondary_checkBox_FramedRoofDetails_MetalCovering);
   	        CheckBox CheckBox_FramedRoofDetails_WoodCovering = (CheckBox) v.findViewById(R.id.Secondary_checkBox_FramedRoofDetails_WoodCovering);
   	        CheckBox CheckBox_FramedRoofDetails_ThatchCovering = (CheckBox) v.findViewById(R.id.Secondary_checkBox_FramedRoofDetails_ThatchCovering);
   	        CheckBox CheckBox_FramedRoofDetails_CanvasCovering = (CheckBox) v.findViewById(R.id.Secondary_checkBox_FramedRoofDetails_CanvasCovering);
   	        CheckBox CheckBox_FramedRoofDetails_Tiles = (CheckBox) v.findViewById(R.id.Secondary_checkBox_FramedRoofDetails_Tiles);
   	        CheckBox CheckBox_FramedRoofDetails_Shingles = (CheckBox) v.findViewById(R.id.Secondary_checkBox_FramedRoofDetails_Shingles);
   	        CheckBox CheckBox_ConstructionMaterialFloor1_NA = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor1_NA);
   	        CheckBox CheckBox_ConstructionMaterialFloor1_Masonry = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor1_Masonry);
   	        CheckBox CheckBox_ConstructionMaterialFloor1_Stone = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor1_Stone);
   	        CheckBox CheckBox_ConstructionMaterialFloor1_CastConcrete = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor1_CastConcrete);
   	        CheckBox CheckBox_ConstructionMaterialFloor1_Wood = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor1_Wood);
   	        CheckBox CheckBox_ConstructionMaterialFloor1_Metal = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor1_Metal);
   	        CheckBox CheckBox_ConstructionMaterialFloor1_Other = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor1_Other);
   	        CheckBox CheckBox_ConstructionMaterialFloor2_NA = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor2_NA);
   	        CheckBox CheckBox_ConstructionMaterialFloor2_Masonry = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor2_Masonry);
   	        CheckBox CheckBox_ConstructionMaterialFloor2_Stone = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor2_Stone);
   	        CheckBox CheckBox_ConstructionMaterialFloor2_CastConcrete = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor2_CastConcrete);
   	        CheckBox CheckBox_ConstructionMaterialFloor2_Wood = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor2_Wood);
   	        CheckBox CheckBox_ConstructionMaterialFloor2_Metal = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor2_Metal);
   	        CheckBox CheckBox_ConstructionMaterialFloor2_Other = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor2_Other);
   	        CheckBox CheckBox_ConstructionMaterialFloor3_NA = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor3_NA);
   	        CheckBox CheckBox_ConstructionMaterialFloor3_Masonry = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor3_Masonry);
   	        CheckBox CheckBox_ConstructionMaterialFloor3_Stone = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor3_Stone);
   	        CheckBox CheckBox_ConstructionMaterialFloor3_CastConcrete = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor3_CastConcrete);
   	        CheckBox CheckBox_ConstructionMaterialFloor3_Wood = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor3_Wood);
   	        CheckBox CheckBox_ConstructionMaterialFloor3_Metal = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor3_Metal);
   	        CheckBox CheckBox_ConstructionMaterialFloor3_Other = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor3_Other);
   	        CheckBox CheckBox_ConstructionMaterialFloor4_NA = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor4_NA);
   	        CheckBox CheckBox_ConstructionMaterialFloor4_Masonry = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor4_Masonry);
   	        CheckBox CheckBox_ConstructionMaterialFloor4_Stone = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor4_Stone);
   	        CheckBox CheckBox_ConstructionMaterialFloor4_CastConcrete = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor4_CastConcrete);
   	        CheckBox CheckBox_ConstructionMaterialFloor4_Wood = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor4_Wood);
   	        CheckBox CheckBox_ConstructionMaterialFloor4_Metal = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor4_Metal);
   	        CheckBox CheckBox_ConstructionMaterialFloor4_Other = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor4_Other);
   	        CheckBox CheckBox_ConstructionMaterialFloor5_NA = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor5_NA);
   	        CheckBox CheckBox_ConstructionMaterialFloor5_Masonry = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor5_Masonry);
   	        CheckBox CheckBox_ConstructionMaterialFloor5_Stone = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor5_Stone);
   	        CheckBox CheckBox_ConstructionMaterialFloor5_CastConcrete = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor5_CastConcrete);
   	        CheckBox CheckBox_ConstructionMaterialFloor5_Wood = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor5_Wood);
   	        CheckBox CheckBox_ConstructionMaterialFloor5_Metal = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor5_Metal);
   	        CheckBox CheckBox_ConstructionMaterialFloor5_Other = (CheckBox) v.findViewById(R.id.Secondary_checkBox_ConstructionMaterialFloor5_Other);

   	        // Extracts String value from the EditText fields
		    String PermanentInteriorPartitionsNote = EditText_PermanentInteriorPartitionsNote.getText().toString();
		    String SemiPermanentInteriorPartitionsNote = EditText_SemiPermanentInteriorPartitionsNote.getText().toString();
		    String FoundationNote = EditText_FoundationNote.getText().toString();
		    String FoundationConstructionMaterialNote = EditText_FoundationConstructionMaterialNote.getText().toString();
		    String RoofSystemTypeNote = EditText_RoofSystemTypeNote.getText().toString();
		    String ConstructionMaterialFloor1Note = EditText_ConstructionMaterialFloor1Note.getText().toString();
		    String ConstructionMaterialFloor2Note = EditText_ConstructionMaterialFloor2Note.getText().toString();
		    String ConstructionMaterialFloor3Note = EditText_ConstructionMaterialFloor3Note.getText().toString();
		    String ConstructionMaterialFloor4Note = EditText_ConstructionMaterialFloor4Note.getText().toString();
		    String ConstructionMaterialFloor5Note = EditText_ConstructionMaterialFloor5Note.getText().toString();
		   
  	        // Extracts value from the CheckBox fields
		    String PermanentInteriorPartitions_Masonry = boolToString(CheckBox_PermanentInteriorPartitions_Masonry.isChecked());
		    String PermanentInteriorPartitions_Stone = boolToString(CheckBox_PermanentInteriorPartitions_Stone.isChecked());
		    String PermanentInteriorPartitions_Earth = boolToString(CheckBox_PermanentInteriorPartitions_Earth.isChecked());
		    String PermanentInteriorPartitions_Wood = boolToString(CheckBox_PermanentInteriorPartitions_Wood.isChecked());
		    String PermanentInteriorPartitions_Metal = boolToString(CheckBox_PermanentInteriorPartitions_Metal.isChecked());
		    String PermanentInteriorPartitions_Other = boolToString(CheckBox_PermanentInteriorPartitions_Other.isChecked());
		    String SemiPermanentInteriorPartitions_Wood = boolToString(CheckBox_SemiPermanentInteriorPartitions_Wood.isChecked());
		    String SemiPermanentInteriorPartitions_Canvas = boolToString(CheckBox_SemiPermanentInteriorPartitions_Canvas.isChecked());
		    String SemiPermanentInteriorPartitions_Other = boolToString(CheckBox_SemiPermanentInteriorPartitions_Other.isChecked());
		    String Foundation_None = boolToString(CheckBox_Foundation_None.isChecked());
		    String Foundation_Mat = boolToString(CheckBox_Foundation_Mat.isChecked());
		    String Foundation_Footings = boolToString(CheckBox_Foundation_Footings.isChecked());
		    String Foundation_ShallowFoundation = boolToString(CheckBox_Foundation_ShallowFoundation.isChecked());
		    String Foundation_DeepFoundation = boolToString(CheckBox_Foundation_DeepFoundation.isChecked());
		    String Foundation_ElevatedOnPiles = boolToString(CheckBox_Foundation_ElevatedOnPiles.isChecked());
		    String Foundation_Other = boolToString(CheckBox_Foundation_Other.isChecked());
		    String FoundationConstructionMaterial_NA = boolToString(CheckBox_FoundationConstructionMaterial_NA.isChecked());
		    String FoundationConstructionMaterial_Masonry = boolToString(CheckBox_FoundationConstructionMaterial_Masonry.isChecked());
		    String FoundationConstructionMaterial_Stone = boolToString(CheckBox_FoundationConstructionMaterial_Stone.isChecked());
		    String FoundationConstructionMaterial_CastConcrete = boolToString(CheckBox_FoundationConstructionMaterial_CastConcrete.isChecked());
		    String FoundationConstructionMaterial_Wood = boolToString(CheckBox_FoundationConstructionMaterial_Wood.isChecked());
		    String FoundationConstructionMaterial_Metal = boolToString(CheckBox_FoundationConstructionMaterial_Metal.isChecked());
		    String FoundationConstructionMaterial_Other = boolToString(CheckBox_FoundationConstructionMaterial_Other.isChecked());
		    String RoofSystem_ConcreteSlab = boolToString(CheckBox_RoofSystem_ConcreteSlab.isChecked());
		    String RoofSystem_Framed = boolToString(CheckBox_RoofSystem_Framed.isChecked());
		    String RoofSystem_Other = boolToString(CheckBox_RoofSystem_Other.isChecked());
		    String ConcreteSlabRoofDetails_MasonryPresence = boolToString(CheckBox_ConcreteSlabRoofDetails_MasonryPresence.isChecked());
		    String FramedRoofDetails_MetalFraming = boolToString(CheckBox_FramedRoofDetails_MetalFraming.isChecked());
		    String FramedRoofDetails_WoodFraming = boolToString(CheckBox_FramedRoofDetails_WoodFraming.isChecked());
		    String FramedRoofDetails_MetalCovering = boolToString(CheckBox_FramedRoofDetails_MetalCovering.isChecked());
		    String FramedRoofDetails_WoodCovering = boolToString(CheckBox_FramedRoofDetails_WoodCovering.isChecked());
		    String FramedRoofDetails_ThatchCovering = boolToString(CheckBox_FramedRoofDetails_ThatchCovering.isChecked());
		    String FramedRoofDetails_CanvasCovering = boolToString(CheckBox_FramedRoofDetails_CanvasCovering.isChecked());
		    String FramedRoofDetails_Tiles = boolToString(CheckBox_FramedRoofDetails_Tiles.isChecked());
		    String FramedRoofDetails_Shingles = boolToString(CheckBox_FramedRoofDetails_Shingles.isChecked());
		    String ConstructionMaterialFloor1_NA = boolToString(CheckBox_ConstructionMaterialFloor1_NA.isChecked());
		    String ConstructionMaterialFloor1_Masonry = boolToString(CheckBox_ConstructionMaterialFloor1_Masonry.isChecked());
		    String ConstructionMaterialFloor1_Stone = boolToString(CheckBox_ConstructionMaterialFloor1_Stone.isChecked());
		    String ConstructionMaterialFloor1_CastConcrete = boolToString(CheckBox_ConstructionMaterialFloor1_CastConcrete.isChecked());
		    String ConstructionMaterialFloor1_Wood = boolToString(CheckBox_ConstructionMaterialFloor1_Wood.isChecked());
		    String ConstructionMaterialFloor1_Metal = boolToString(CheckBox_ConstructionMaterialFloor1_Metal.isChecked());
		    String ConstructionMaterialFloor1_Other = boolToString(CheckBox_ConstructionMaterialFloor1_Other.isChecked());
		    String ConstructionMaterialFloor2_NA = boolToString(CheckBox_ConstructionMaterialFloor2_NA.isChecked());
		    String ConstructionMaterialFloor2_Masonry = boolToString(CheckBox_ConstructionMaterialFloor2_Masonry.isChecked());
		    String ConstructionMaterialFloor2_Stone = boolToString(CheckBox_ConstructionMaterialFloor2_Stone.isChecked());
		    String ConstructionMaterialFloor2_CastConcrete = boolToString(CheckBox_ConstructionMaterialFloor2_CastConcrete.isChecked());
		    String ConstructionMaterialFloor2_Wood = boolToString(CheckBox_ConstructionMaterialFloor2_Wood.isChecked());
		    String ConstructionMaterialFloor2_Metal = boolToString(CheckBox_ConstructionMaterialFloor2_Metal.isChecked());
		    String ConstructionMaterialFloor2_Other = boolToString(CheckBox_ConstructionMaterialFloor2_Other.isChecked());
		    String ConstructionMaterialFloor3_NA = boolToString(CheckBox_ConstructionMaterialFloor3_NA.isChecked());
		    String ConstructionMaterialFloor3_Masonry = boolToString(CheckBox_ConstructionMaterialFloor3_Masonry.isChecked());
		    String ConstructionMaterialFloor3_Stone = boolToString(CheckBox_ConstructionMaterialFloor3_Stone.isChecked());
		    String ConstructionMaterialFloor3_CastConcrete = boolToString(CheckBox_ConstructionMaterialFloor3_CastConcrete.isChecked());
		    String ConstructionMaterialFloor3_Wood = boolToString(CheckBox_ConstructionMaterialFloor3_Wood.isChecked());
		    String ConstructionMaterialFloor3_Metal = boolToString(CheckBox_ConstructionMaterialFloor3_Metal.isChecked());
		    String ConstructionMaterialFloor3_Other = boolToString(CheckBox_ConstructionMaterialFloor3_Other.isChecked());
		    String ConstructionMaterialFloor4_NA = boolToString(CheckBox_ConstructionMaterialFloor4_NA.isChecked());
		    String ConstructionMaterialFloor4_Masonry = boolToString(CheckBox_ConstructionMaterialFloor4_Masonry.isChecked());
		    String ConstructionMaterialFloor4_Stone = boolToString(CheckBox_ConstructionMaterialFloor4_Stone.isChecked());
		    String ConstructionMaterialFloor4_CastConcrete = boolToString(CheckBox_ConstructionMaterialFloor4_CastConcrete.isChecked());
		    String ConstructionMaterialFloor4_Wood = boolToString(CheckBox_ConstructionMaterialFloor4_Wood.isChecked());
		    String ConstructionMaterialFloor4_Metal = boolToString(CheckBox_ConstructionMaterialFloor4_Metal.isChecked());
		    String ConstructionMaterialFloor4_Other = boolToString(CheckBox_ConstructionMaterialFloor4_Other.isChecked());
		    String ConstructionMaterialFloor5_NA = boolToString(CheckBox_ConstructionMaterialFloor5_NA.isChecked());
		    String ConstructionMaterialFloor5_Masonry = boolToString(CheckBox_ConstructionMaterialFloor5_Masonry.isChecked());
		    String ConstructionMaterialFloor5_Stone = boolToString(CheckBox_ConstructionMaterialFloor5_Stone.isChecked());
		    String ConstructionMaterialFloor5_CastConcrete = boolToString(CheckBox_ConstructionMaterialFloor5_CastConcrete.isChecked());
		    String ConstructionMaterialFloor5_Wood = boolToString(CheckBox_ConstructionMaterialFloor5_Wood.isChecked());
		    String ConstructionMaterialFloor5_Metal = boolToString(CheckBox_ConstructionMaterialFloor5_Metal.isChecked());
		    String ConstructionMaterialFloor5_Other = boolToString(CheckBox_ConstructionMaterialFloor5_Other.isChecked());
   			
  	  		String[][] databaseUpdatesSecondary= {
	  	  			{SqlHelper.COLUMN_STRUCTURE_PERMANENT_INTERIOR_PARTITIONS_NOTE, PermanentInteriorPartitionsNote},
	  	  			{SqlHelper.COLUMN_STRUCTURE_SEMI_PERMANENT_INTERIOR_PARTITIONS_NOTE, SemiPermanentInteriorPartitionsNote},
	  	  			{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_SYSTEM_NOTE, FoundationNote},
	  	  			{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_CONSTRUCTION_MATERIAL_NOTE, FoundationConstructionMaterialNote},
	  	  			{SqlHelper.COLUMN_STRUCTURE_ROOF_TYPE_NOTE, RoofSystemTypeNote},
	  	  			{SqlHelper.COLUMN_STRUCTURE_FLOOR_1_CONSTRUCTION_MATERIAL_NOTE, ConstructionMaterialFloor1Note},
	  	  			{SqlHelper.COLUMN_STRUCTURE_FLOOR_2_CONSTRUCTION_MATERIAL_NOTE, ConstructionMaterialFloor2Note},
	  	  			{SqlHelper.COLUMN_STRUCTURE_FLOOR_3_CONSTRUCTION_MATERIAL_NOTE, ConstructionMaterialFloor3Note},
	  	  			{SqlHelper.COLUMN_STRUCTURE_FLOOR_4_CONSTRUCTION_MATERIAL_NOTE, ConstructionMaterialFloor4Note},
	  	  			{SqlHelper.COLUMN_STRUCTURE_FLOOR_5_CONSTRUCTION_MATERIAL_NOTE, ConstructionMaterialFloor5Note},
	  	  			{SqlHelper.COLUMN_STRUCTURE_PERMANENT_INTERIOR_PARTITIONS_MASONRY ,PermanentInteriorPartitions_Masonry},
			  	  	{SqlHelper.COLUMN_STRUCTURE_PERMANENT_INTERIOR_PARTITIONS_STONE ,PermanentInteriorPartitions_Stone},
			  	  	{SqlHelper.COLUMN_STRUCTURE_PERMANENT_INTERIOR_PARTITIONS_EARTH ,PermanentInteriorPartitions_Earth},
			  	  	{SqlHelper.COLUMN_STRUCTURE_PERMANENT_INTERIOR_PARTITIONS_WOOD ,PermanentInteriorPartitions_Wood},
			  	  	{SqlHelper.COLUMN_STRUCTURE_PERMANENT_INTERIOR_PARTITIONS_METAL,PermanentInteriorPartitions_Metal},
			  	  	{SqlHelper.COLUMN_STRUCTURE_PERMANENT_INTERIOR_PARTITIONS_OTHER,PermanentInteriorPartitions_Other},
			  	  	{SqlHelper.COLUMN_STRUCTURE_SEMI_PERMANENT_INTERIOR_PARTITIONS_WOOD,SemiPermanentInteriorPartitions_Wood},
			  	  	{SqlHelper.COLUMN_STRUCTURE_SEMI_PERMANENT_INTERIOR_PARTITIONS_CANVAS,SemiPermanentInteriorPartitions_Canvas},
			  	  	{SqlHelper.COLUMN_STRUCTURE_SEMI_PERMANENT_INTERIOR_PARTITIONS_OTHER,SemiPermanentInteriorPartitions_Other},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_SYSTEM_NONE,Foundation_None},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_SYSTEM_MAT,Foundation_Mat},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_SYSTEM_FOOTINGS,Foundation_Footings},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_SYSTEM_SHALLOW_FOUNDATION,Foundation_ShallowFoundation},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_SYSTEM_DEEP_FOUNDATION ,Foundation_DeepFoundation},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_SYSTEM_ELEVATED_ON_PILES ,Foundation_ElevatedOnPiles},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_SYSTEM_OTHER ,Foundation_Other},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_CONSTRUCTION_MATERIAL_NA ,FoundationConstructionMaterial_NA},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_CONSTRUCTION_MATERIAL_MASONRY,FoundationConstructionMaterial_Masonry},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_CONSTRUCTION_MATERIAL_STONE,FoundationConstructionMaterial_Stone},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_CONSTRUCTION_MATERIAL_CAST_CONCRETE,FoundationConstructionMaterial_CastConcrete},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_CONSTRUCTION_MATERIAL_WOOD,FoundationConstructionMaterial_Wood},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_CONSTRUCTION_MATERIAL_METAL,FoundationConstructionMaterial_Metal},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FOUNDATION_CONSTRUCTION_MATERIAL_OTHER,FoundationConstructionMaterial_Other},
			  	  	{SqlHelper.COLUMN_STRUCTURE_ROOF_TYPE_CONCRETE_SLAB,RoofSystem_ConcreteSlab},
			  	  	{SqlHelper.COLUMN_STRUCTURE_ROOF_TYPE_FRAMED,RoofSystem_Framed},
			  	  	{SqlHelper.COLUMN_STRUCTURE_ROOF_TYPE_OTHER,RoofSystem_Other},
			  	  	{SqlHelper.COLUMN_STRUCTURE_CONCRETE_SLAB_ROOF_PRESENCE_OF_MASONRY,ConcreteSlabRoofDetails_MasonryPresence},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FRAMED_ROOF_METAL_FRAMING,FramedRoofDetails_MetalFraming},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FRAMED_ROOF_WOOD_FRAMING ,FramedRoofDetails_WoodFraming},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FRAMED_ROOF_METAL_COVERING,FramedRoofDetails_MetalCovering},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FRAMED_ROOF_WOOD_COVERING,FramedRoofDetails_WoodCovering},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FRAMED_ROOF_THATCH_COVERING,FramedRoofDetails_ThatchCovering},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FRAMED_ROOF_CANVAS_COVERING,FramedRoofDetails_CanvasCovering},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FRAMED_ROOF_TILES,FramedRoofDetails_Tiles},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FRAMED_ROOF_SHINGLES,FramedRoofDetails_Shingles},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_1_CONSTRUCTION_MATERIAL_NA ,ConstructionMaterialFloor1_NA},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_1_CONSTRUCTION_MATERIAL_MASONRY ,ConstructionMaterialFloor1_Masonry},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_1_CONSTRUCTION_MATERIAL_STONE ,ConstructionMaterialFloor1_Stone},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_1_CONSTRUCTION_MATERIAL_CAST_CONCRETE ,ConstructionMaterialFloor1_CastConcrete},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_1_CONSTRUCTION_MATERIAL_WOOD ,ConstructionMaterialFloor1_Wood},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_1_CONSTRUCTION_MATERIAL_METAL ,ConstructionMaterialFloor1_Metal},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_1_CONSTRUCTION_MATERIAL_OTHER ,ConstructionMaterialFloor1_Other},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_2_CONSTRUCTION_MATERIAL_NA ,ConstructionMaterialFloor2_NA},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_2_CONSTRUCTION_MATERIAL_MASONRY ,ConstructionMaterialFloor2_Masonry},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_2_CONSTRUCTION_MATERIAL_STONE ,ConstructionMaterialFloor2_Stone},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_2_CONSTRUCTION_MATERIAL_CAST_CONCRETE ,ConstructionMaterialFloor2_CastConcrete},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_2_CONSTRUCTION_MATERIAL_WOOD ,ConstructionMaterialFloor2_Wood},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_2_CONSTRUCTION_MATERIAL_METAL ,ConstructionMaterialFloor2_Metal},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_2_CONSTRUCTION_MATERIAL_OTHER ,ConstructionMaterialFloor2_Other},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_3_CONSTRUCTION_MATERIAL_NA ,ConstructionMaterialFloor3_NA},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_3_CONSTRUCTION_MATERIAL_MASONRY ,ConstructionMaterialFloor3_Masonry},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_3_CONSTRUCTION_MATERIAL_STONE ,ConstructionMaterialFloor3_Stone},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_3_CONSTRUCTION_MATERIAL_CAST_CONCRETE ,ConstructionMaterialFloor3_CastConcrete},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_3_CONSTRUCTION_MATERIAL_WOOD ,ConstructionMaterialFloor3_Wood},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_3_CONSTRUCTION_MATERIAL_METAL,ConstructionMaterialFloor3_Metal},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_3_CONSTRUCTION_MATERIAL_OTHER,ConstructionMaterialFloor3_Other},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_4_CONSTRUCTION_MATERIAL_NA,ConstructionMaterialFloor4_NA},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_4_CONSTRUCTION_MATERIAL_MASONRY,ConstructionMaterialFloor4_Masonry},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_4_CONSTRUCTION_MATERIAL_STONE,ConstructionMaterialFloor4_Stone},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_4_CONSTRUCTION_MATERIAL_CAST_CONCRETE,ConstructionMaterialFloor4_CastConcrete},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_4_CONSTRUCTION_MATERIAL_WOOD,ConstructionMaterialFloor4_Wood},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_4_CONSTRUCTION_MATERIAL_METAL,ConstructionMaterialFloor4_Metal},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_4_CONSTRUCTION_MATERIAL_OTHER,ConstructionMaterialFloor4_Other},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_5_CONSTRUCTION_MATERIAL_NA ,ConstructionMaterialFloor5_NA},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_5_CONSTRUCTION_MATERIAL_MASONRY,ConstructionMaterialFloor5_Masonry},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_5_CONSTRUCTION_MATERIAL_STONE,ConstructionMaterialFloor5_Stone},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_5_CONSTRUCTION_MATERIAL_CAST_CONCRETE,ConstructionMaterialFloor5_CastConcrete},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_5_CONSTRUCTION_MATERIAL_WOOD ,ConstructionMaterialFloor5_Wood},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_5_CONSTRUCTION_MATERIAL_METAL,ConstructionMaterialFloor5_Metal},
			  	  	{SqlHelper.COLUMN_STRUCTURE_FLOOR_5_CONSTRUCTION_MATERIAL_OTHER,ConstructionMaterialFloor5_Other}
	  	  			};
	  	  		
                // Updates the entries in the database
                updateStructureDB(databaseUpdatesSecondary);
   		
                break;
          // 	===============================================================================================  
                
   		default: // Update form tab ID was not found
   			// current does nothing
   			break;
   		}
       
    }
    
    public static String boolToString(Boolean b) {
  	  if (b)
  	    return "1";
  	  if (!b)
  	    return "0";
  	  throw new IllegalArgumentException(b+" is not a bool. Only 1 and 0 are.");
  }
    
    public static void updateStructureDB(String[][] databaseUpdates){
        // Push the changes to the database
        utilsHelper.openDatabase();
        utilsHelper.dbHelper.updateStructureEntry(databaseUpdates, structureID );
        utilsHelper.closeDatabase();
    }
    @Override
    protected Dialog onCreateDialog(int id) {
		
            switch (id) {

            case STRUCTURE_NOTE_DIALOG_ID:
            	AlertDialog.Builder builder = new AlertDialog.Builder(this);
                
                // Inflates the custom xml file layout create_structure_dialog.xml
                LayoutInflater inflater = this.getLayoutInflater();    
                View  dialogView  =  inflater.inflate(R.layout.structure_notes_dialog,null);
                builder.setView(dialogView);
                
                builder.setTitle(R.string.structure_notes_dialog_title);
                //builder2.setIcon(R.drawable.icon);    
                
                final EditText EditText_structureNotes = (EditText) dialogView.findViewById(R.id.structureNotes);     
                
                builder.setPositiveButton(R.string.save,
                		          
                                new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            
                                        	// Defines strings input in by the user
                                            String structureNotes = EditText_structureNotes.getText().toString();
                                        	
                                            // Updates the user-defined structure to  the database
                                            //structureID = adapter.getCursor().getInt(adapter.getCursor().getColumnIndex(SqlHelper.COLUMN_ID));
                                            String[][] databaseUpdates = {
                                                    {SqlHelper.COLUMN_STRUCTURE_NOTES, structureNotes} 
                                                    };

                                            utilsHelper.openDatabase();                	
                                            utilsHelper.dbHelper.updateStructureEntry(databaseUpdates, structureID );
                                            structureCursor = utilsHelper.getStructureCursor(structureID);
                                           // utilsHelper.closeDatabase();                	
                                            
                                            return;
                                        }
                                });

                builder.setNegativeButton(android.R.string.cancel,
                                new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {                                                   
                                                return;
                                        }
                                });    
                
                Dialog structureDialogEdit = builder.create();

                return structureDialogEdit;
        }
            
            return null;
                                                  
    }
    
    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {   	
 
        switch (id) { 
            case STRUCTURE_NOTE_DIALOG_ID:
            	
            	final EditText structureNotes = (EditText) dialog.findViewById(R.id.structureNotes);     
            	
        		//Clear the EditText fields
        		String oldNotes= structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NOTES));
        		
        		structureNotes.setText(oldNotes); 
        		structureNotes.requestFocus();
                break;            	
                
            default:
            	Log.d(LAYOUT_INFLATER_SERVICE, "Wrong Structure DIALOG ID");
            	break;
        }
    }
}
